#include "CharacterCamp.h"
#include "Common/TableData/CampGodsCfg.h"
//#include "base/core/utility.h"
#include "base/core/char_operation.h"
#include "Common/Utility/ServerTime.h"

CharacterCamp::CharacterCamp()
{
	Clear();
}

CharacterCamp::~CharacterCamp()
{}

void CharacterCamp::Clear()
{
	m_nCharID = 0;
	m_needSave = false;
	m_nTotalContribute = 0;
	m_nLastJoin = 0;
	m_nChangeTick = 0;
	m_nDividendFlag = false;
	m_nDividendTime = 0;
	m_nVoteFlag = false;
	m_nChallengeFlag = false;
	m_nElectioneerId = 0;
	m_nGodsID = 0;
	m_nGodsTimes = 0;
	m_nGodsLevel = 0;
	m_setKiller.clear();
	m_nLastLogout = 0;
	m_nPrestige = 0;
	m_nDevout = 0;
	m_prayMap.clear();
}

void CharacterCamp::Init(uint32_t campConfigId)
{
	const CampGodsCfgMap *pGodsMap = g_GetCampGodsCfgTable()->GetCampGodsCfgMap();
	if (pGodsMap == nullptr)
	{
		LogErrFmtPrint("[center] CharacterCamp::Init... pGodsMap == nullptr");
		return;
	}
	m_prayMap.clear();
	for (CampGodsCfgMap::const_iterator ib = pGodsMap->begin(), ie = pGodsMap->end(); ib != ie; ++ib)
	{
		const CampGodsCfgInfo& pGodInfo = ib->second;
		if (campConfigId != (uint32_t)pGodInfo.campPartition)		continue;
		SCampPrayInfo prayInfo;
		VEC_INT32 attList;
		StringSplit2Int(pGodInfo.abilityId, ',', attList);
		size_t size = attList.size();
		for (size_t i = 0; i != size; ++i)
		{
			prayInfo.attrMap[attList.at(i)] = 0;
		}
		m_prayMap[ib->first] = prayInfo;
	}
}

bool CharacterCamp::NeedSave()
{
	return m_needSave;
}
void CharacterCamp::NeedSave(bool save)
{
	m_needSave = save;
}

void CharacterCamp::SaveData(CharacterCampDBData *protoSave)
{
	//if (!protoSave)
	//	return;

	protoSave->set_charid(m_nCharID);
	protoSave->set_total_contribute(m_nTotalContribute);
	protoSave->set_last_join(m_nLastJoin);
	protoSave->set_god_id(m_nGodsID);
	protoSave->set_electioneer_id(m_nElectioneerId);
	protoSave->set_dividend_flag(m_nDividendFlag);
	protoSave->set_dividend_time(m_nDividendTime);
	protoSave->set_vote_flag(m_nVoteFlag);
	protoSave->set_challenge_flag(m_nChallengeFlag);
	protoSave->set_last_logout(g_GetGlobalServerTime()->UnixSec());
	protoSave->set_change_tick(m_nChangeTick);

	for (CampPrayInfoMap::iterator ib = m_prayMap.begin(), ie = m_prayMap.end(); ib != ie; ++ib)
	{
		SCampPrayInfo &prayInfo = ib->second;
		CharacterCampPrayInfo *pPrayProto = protoSave->add_prayinfo();
		pPrayProto->set_godid(ib->first);
		pPrayProto->set_devout(prayInfo.devout);
		for (MAP_UINT32_UINT32::iterator iteb = prayInfo.attrMap.begin(), itee = prayInfo.attrMap.end(); iteb != itee; ++iteb)
		{
			CharacterCampPrayInfo::Attr *pAttr = pPrayProto->add_attinfo();
			pAttr->set_attrid(iteb->first);
			pAttr->set_att(iteb->second);
		}
	}

	m_needSave = false;
}

void CharacterCamp::LoadData(const CharacterCampDBData &charData)
{
	m_nTotalContribute = charData.total_contribute();
	m_nLastJoin = charData.last_join();
	m_nGodsID = charData.god_id();
	m_nElectioneerId = charData.electioneer_id();
	m_nDividendFlag = charData.dividend_flag();
	m_nDividendTime = charData.dividend_time();
	m_nVoteFlag = charData.vote_flag();
	m_nChallengeFlag = charData.challenge_flag();
	m_nLastLogout = charData.last_logout();
	m_nChangeTick = charData.change_tick();
}

CharIDType CharacterCamp::CharID()
{
	return m_nCharID;
}
void CharacterCamp::CharID(CharIDType charID)
{
	m_nCharID = charID;
}

uint32_t CharacterCamp::TotalContribute()
{
	return m_nTotalContribute;
}
void CharacterCamp::TotalContribute(uint32_t nTotalContribute)
{
	m_nTotalContribute = nTotalContribute;
}

uint64_t CharacterCamp::LastJoin()
{
	return m_nLastJoin;
}
void CharacterCamp::LastJoin(uint64_t nLastJoin)
{
	m_nLastJoin = nLastJoin;
}

uint64_t CharacterCamp::ChangeTick()
{
	return m_nChangeTick;
}
void CharacterCamp::ChangeTick(uint64_t nChangeTick)
{
	m_nChangeTick = nChangeTick;
}

uint32_t CharacterCamp::GodsID()
{
	return m_nGodsID;
}
void CharacterCamp::GodsID(uint32_t nGodsID)
{
	m_nGodsID = nGodsID;
}

uint32_t CharacterCamp::GodsTimes()
{
	return m_nGodsTimes;
}
void CharacterCamp::GodsTimes(uint32_t nGodsTimes)
{
	m_nGodsTimes = nGodsTimes;
}

uint32_t CharacterCamp::GodsLevel()
{
	return m_nGodsLevel;
}
void CharacterCamp::GodsLevel(uint32_t nGodsLevel)
{
	m_nGodsLevel = nGodsLevel;
}

CharIDType CharacterCamp::ElectioneerId()
{
	return m_nElectioneerId;
}
void CharacterCamp::ElectioneerId(CharIDType nElectioneerId)
{
	m_nElectioneerId = nElectioneerId;
}

uint64_t CharacterCamp::LastLogout()
{
	return m_nLastLogout;
}
void CharacterCamp::LastLogout(uint64_t nLastLogout)
{
	m_nLastLogout = nLastLogout;
}

uint32_t CharacterCamp::Prestige()
{
	return m_nPrestige;
}
void CharacterCamp::Prestige(uint32_t nPrestige)
{
	m_nPrestige = nPrestige;
}

uint32_t CharacterCamp::Devout()
{
	return m_nDevout;
}
void CharacterCamp::Devout(uint32_t nDevout)
{
	m_nDevout = nDevout;
}

bool CharacterCamp::DividendFlag()
{
	return m_nDividendFlag;
}
void CharacterCamp::DividendFlag(bool nDividendFlag)
{
	m_nDividendFlag = nDividendFlag;
}

uint64_t CharacterCamp::DividendTime()
{
	return m_nDividendTime;
}

void CharacterCamp::DividendTime(uint64_t dividendTime)
{
	m_nDividendTime = dividendTime;
}

bool CharacterCamp::VoteFlag()
{
	return m_nVoteFlag;
}
void CharacterCamp::VoteFlag(bool nVoteFlag)
{
	m_nVoteFlag = nVoteFlag;
}

bool CharacterCamp::ChallengeFlag()
{
	return m_nChallengeFlag;
}
void CharacterCamp::ChallengeFlag(bool nChallengeFlag)
{
	m_nChallengeFlag = nChallengeFlag;
}

SCampPrayInfo* CharacterCamp::GetPray(uint32_t godId)
{
	CampPrayInfoMap::iterator it = m_prayMap.find(godId);
	return it != m_prayMap.end() ? &it->second : nullptr;
}

void CharacterCamp::InsertKiller(CharIDType charID)
{
	m_setKiller.insert(charID);
}

bool CharacterCamp::IsExist(CharIDType charID)
{
	return m_setKiller.count(charID) > 0;
}

void CharacterCamp::ClearKiller()
{
	m_setKiller.clear();
}

void CharacterCamp::ResetDay()
{
	m_nPrestige = 0;
	m_nGodsID = 0;
	m_nGodsTimes = 0;
	m_nGodsLevel = 0;
	m_setKiller.clear();
	m_nTotalContribute = 0;
}

void CharacterCamp::ResetEleInfo()
{
	m_nVoteFlag = false;
	m_nChallengeFlag = false;
}
